package common.service.base.common;

import javax.servlet.http.HttpServletRequest;

public class ReqHeader {
	
	/**
	 * 请求头
	 */
	private String PLAT;	//操作系统平台
	private String TOKEN;	//sessionId
	private String CDID;   //设备id
	private String APPVERSION;	//app应用版本
	private String SIGN;	//验签
	private String IP;	//IP地址

	private String REQTYPE;//请求类型(可为空，为空时为原生)
	private String MOBILEMODEL; //手机型号
	
 	public String getMOBILEMODEL() {
		return MOBILEMODEL;
	}
	public void setMOBILEMODEL(String mOBILEMODEL) {
		MOBILEMODEL = mOBILEMODEL;
	}
	public String getPLAT() {
		return PLAT;
	}
	public void setPLAT(String pLAT) {
		PLAT = pLAT;
	}
	public String getAPPVERSION() {
		return APPVERSION;
	}
	public void setAPPVERSION(String aPPVERSION) {
		APPVERSION = aPPVERSION;
	}
	public String getIP() {
		return IP;
	}
	public void setIP(String iP) {
		IP = iP;
	}
	

	public String getIP(HttpServletRequest request){
		if(request != null){
			String ip = request.getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("http_client_ip");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("X-Real-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			// 如果是多级代理，那么取第一个ip为客户ip
			if (ip != null && ip.indexOf(",") != -1) {
				ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
			}
//			String ipReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
//	                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
//	                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
//	                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";  
//	  
//	        Pattern pattern = Pattern.compile(ipReg);  
//	        Matcher matcher = pattern.matcher(ip);  
//	        if(!matcher.matches()){
//	        	ip = "";
//	        }
	        return ip;
		}else{
			return "0.0.0.0";
		}
	}
	public String getREQTYPE() {
		return REQTYPE;
	}
	public void setREQTYPE(String rEQTYPE) {
		REQTYPE = rEQTYPE;
	}

	public String getTOKEN() {
		return TOKEN;
	}

	public void setTOKEN(String TOKEN) {
		this.TOKEN = TOKEN;
	}

	public String getCDID() {
		return CDID;
	}

	public void setCDID(String CDID) {
		this.CDID = CDID;
	}

	public String getSIGN() {
		return SIGN;
	}

	public void setSIGN(String SIGN) {
		this.SIGN = SIGN;
	}
}
